import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutterdemo/navigator/page2.dart';
import 'package:flutterdemo/navigator/page3.dart';
import 'package:flutterdemo/navigator/page4.dart';
import 'package:flutterdemo/notFoundPage.dart';
import 'package:flutterdemo/pages/tab1/splash_screen.dart';
import 'package:flutterdemo/pages/tab1/statefulllesswidget.dart';
import 'package:flutterdemo/pages/tab_navigator.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';

import 'model/providermodel/userinfobasic.dart';

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: UserInfo()),
      ],
      child: MyApp(),
    ),
  );
  // 安卓设置顶部状态栏颜色
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      // 顶部状态栏字体颜色
//        statusBarBrightness: Brightness.light,
//        statusBarIconBrightness: Brightness.dark
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);

    print("systemUiOverlayStyle");
  }
}

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    String _brightness = Provider.of<UserInfo>(context).brightness;
    return OKToast(
      dismissOtherOnShow: true,
      child: MaterialApp(
        title: 'Flutter Demo',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          brightness:
              _brightness == "light" ? Brightness.light : Brightness.dark,
          // fontFamily: 'Maobi', //全局设置字体family
          // This is the theme of your application.
          //
          // Try running your application with "flutter run". You'll see the
          // application has a blue toolbar. Then, without quitting the app, try
          // changing the primarySwatch below to Colors.green and then invoke
          // "hot reload" (press "r" in the console where you ran "flutter run",
          // or simply save your changes to "hot reload" in a Flutter IDE).
          // Notice that the counter didn't reset back to zero; the application
          // is not restarted.
          // primarySwatch: Colors.red,
          // primaryColor: Colors.red,
          // accentColor: Colors.red,
        ),
        // home: TabNavigator(),
        initialRoute: '/',
        // 定义路由对象
        routes: {
          '/': (context) => SplashScreen(),
          '/tabNavigator': (context) => TabNavigator(),
          '/page2': (context) => Page2(),
          '/page3': (context) => Page3(),
          '/page4': (context) => Page4(),
          '/statefulWidgetAndStatelessWidget': (context) =>
              StatefulWidgetAndStatelessWidget(),
        },
        // 404页面
        onUnknownRoute: (RouteSettings setting) {
          String name = setting.name;
          print("onUnknownRoute:$name");
          return new MaterialPageRoute(builder: (context) {
            return new NotFoundPage();
          });
        },
      ),
    );
  }
}

// // // dart 中的可见性以模块（library）为单位，一般每一个dart文件就是一个库
// // import 'package:flutter_summary/person.dart';

// // void main() {
// //   // Person person = new Person(10, 20, 'nan');
// //   // person?.mj();
// //   // print(person?.e);
// //   // num mj = person.mj();
// //   // print(mj);
// //   // var person;
// //   // person = 'zshk';
// //   // person = new Person(10, 20, 'zsk');
// //   // 类型转换 as 关键字
// //   // (person as Person).work();
// //   // 是否指定类型 is,is!;

// //   // if (person is Person) {
// //   //   print('is');
// //   // } else {
// //   //   print('is!');
// //   // }
// //   // 联级操作 。。
// //   // 必须通过类来访问静态成员（属性和方法）
// //   print(Person.w);
// //   new Person()
// //     ..h = 10
// //     // ..w = 20
// //     ..work();
// // }

// // void main() {
// //   runApp(
// //     MultiProvider(
// //       providers: [
// //         ChangeNotifierProvider.value(value: UserInfo()),
// //       ],
// //       child: MyApp(),
// //     ),
// //   );
// //   // 安卓设置顶部状态栏颜色
// //   if (Platform.isAndroid) {
// //     SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
// //       statusBarColor: Colors.transparent,
// //       // 顶部状态栏字体颜色
// // //        statusBarBrightness: Brightness.light,
// // //        statusBarIconBrightness: Brightness.dark
// //     );
// //     SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);

// //     print("systemUiOverlayStyle");
// //   }
// // }

// // class MyApp extends StatelessWidget {
// //   // This widget is the root of your application.
// //   @override
// //   Widget build(BuildContext context) {
// //     return OKToast(
// //         dismissOtherOnShow: true,
// //         child: MaterialApp(
// //           title: 'Flutter Demo',
// //           debugShowCheckedModeBanner: false,
// //           theme: ThemeData(
// //             // This is the theme of your application.
// //             //
// //             // Try running your application with "flutter run". You'll see the
// //             // application has a blue toolbar. Then, without quitting the app, try
// //             // changing the primarySwatch below to Colors.green and then invoke
// //             // "hot reload" (press "r" in the console where you ran "flutter run",
// //             // or simply save your changes to "hot reload" in a Flutter IDE).
// //             // Notice that the counter didn't reset back to zero; the application
// //             // is not restarted.
// //             primarySwatch: Colors.blue,
// //             // This makes the visual density adapt to the platform that you run
// //             // the app on. For desktop platforms, the controls will be smaller and
// //             // closer together (more dense) than on mobile platforms.
// //             visualDensity: VisualDensity.adaptivePlatformDensity,
// //           ),
// //           // home: TabNavigator(),
// //           routes: {
// //             '/': (context) => SplashScreen(),
// //             '/tabNavigator': (context) => TabNavigator(),
// //             '/page2': (context) => Page2(),
// //             '/page3': (context) => Page3(),
// //             '/page4': (context) => Page4(),
// //             '/statefulWidgetAndStatelessWidget': (context) =>
// //                 StatefulWidgetAndStatelessWidget(),
// //           },
// //         ));
// //   }
// // }

// import 'package:flutter/material.dart';
// import 'package:provider/provider.dart';

// main() {
//   runApp(CartPage());
// }

class CartPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
      child: MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (context) {
            return MainProvide();
          }),
          ChangeNotifierProvider(create: (context) {
            return MainProvide2();
          })
        ],
        child: Column(
          children: <Widget>[Number(), MyButton()],
        ),
      ),
    ));
  }
}

class Number extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    MainProvide provider = Provider.of<MainProvide>(context);
    MainProvide2 provider2 = Provider.of<MainProvide2>(context);
    int curNum = provider.curNum;
    int curNum2 = provider2.curNum;
    return Container(
        margin: EdgeInsets.only(top: 200),
        child: Column(
          children: <Widget>[
            Text('$curNum'),
            Text('$curNum2'),
          ],
        ));
  }
}

class MyButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    MainProvide provider = Provider.of<MainProvide>(context);
    MainProvide2 provider2 = Provider.of<MainProvide2>(context);
    return Container(
      child: RaisedButton(
        onPressed: () {
          provider.add();
          provider2.add();
        },
        child: Text('递增'),
      ),
    );
  }
}

class MainProvide extends ChangeNotifier {
  int curNum = 0;
  add() {
    curNum += 1;
    notifyListeners();
  }

  minus() {
    curNum -= 1;
    notifyListeners();
  }
}

class MainProvide2 extends ChangeNotifier {
  int curNum = 0;
  add() {
    curNum += 1;
    notifyListeners();
  }

  minus() {
    curNum -= 1;
    notifyListeners();
  }
}
